package org.meruvian.yama.controller;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.meruvian.yama.service.impl.JSONConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * This is the main Spring MVC controller for this sample webapp.
 */
@Controller
public class JSONIntegrationController {

    @Autowired
    JSONConsumerService serviceBean;
    
    @RequestMapping("/joget")
    public String indexJoget() {
        return "joget/indexJoget";
    }
    
    
    //get joget processlist
    @RequestMapping("/joget/process/list")
    public String getProcessDefList(ModelMap map, @RequestParam(required = false) String username, @RequestParam(required = false) String password) {
        Map<String, Object> processMap = serviceBean.getList("processList",username, password);
        map.addAttribute("processMap", processMap);
        map.addAttribute("urlEncoder", serviceBean);
        return "joget/getProcessDefList";
    }
    
    //get joget inbox
    @RequestMapping("/joget/inbox")
    public String getInbox(ModelMap map, @RequestParam(required = false) String username, @RequestParam(required = false) String password) {
        Map<String, Object> processMap = serviceBean.getList("inbox", username, password);
        map.addAttribute("processMap", processMap);
        map.addAttribute("urlEncoder", serviceBean);
        return "joget/inbox";
    }
    
    //get joget completed process list
    @RequestMapping("/joget/completed/process/list")
    public String getCompletedProcess(ModelMap map, @RequestParam(required = false) String username, @RequestParam(required = false) String password) {
        Map<String, Object> processMap = serviceBean.getList("completedProcess", username, password);
        map.addAttribute("processMap", processMap);
//        map.addAttribute("urlEncoder", serviceBean);
        return "joget/completedProcess";
    }

    //get joget running process list
    @RequestMapping("/joget/running/process/list")
    public String getRunningProcess(ModelMap map, @RequestParam(required = false) String username, @RequestParam(required = false) String password) {
        Map<String, Object> processMap = serviceBean.getList("runningProcess", username, password);
        map.addAttribute("processMap", processMap);
        map.addAttribute("urlEncoder", serviceBean);
        return "joget/runningProcess";
    }
    
    //view activity 
    @RequestMapping("/joget/monitoring/activity/view/{activityId}")
    public String viewActivity(ModelMap map, @PathVariable("activityId") String activityId, @RequestParam(required = false) String username, @RequestParam(required = false) String password) {
        Map<String, Object> process = serviceBean.getListById("viewActivity", activityId, username, password);
        map.addAttribute("process", process);
        return "joget/viewActivity";
    }
    
    //view activity from inbox
    @RequestMapping("/joget/inbox/activity/view/{activityId}")
    public String viewInboxActivity(ModelMap map, @PathVariable("activityId") String activityId, @RequestParam(required = false) String username, @RequestParam(required = false) String password) {
        Map<String, Object> process = serviceBean.getListById("viewInboxActivity", activityId, username, password);
        map.addAttribute("process", process);
        return "joget/viewInboxActivity";
    }
    
    //view running process
    @RequestMapping("/joget/running/process/view/{processId}")
    public String viewRunningProcess(ModelMap map, @PathVariable("processId") String processId, @RequestParam(required = false) String username, @RequestParam(required = false) String password) {
        Map<String, Object> processMap = serviceBean.getListById("view", processId, username, password);
        Map<String, Object> process = serviceBean.getListById("viewProcess", processId, username, password);
        map.addAttribute("processMap", processMap);
        map.addAttribute("process", process);
//        map.addAttribute("urlEncoder", serviceBean);
        return "joget/viewProcess";
    }
    
    // abort running process
    @RequestMapping("/joget/running/process/abort/{processId}")
    public String abortRunningProcess(ModelMap map, @PathVariable("processId") String processId, @RequestParam(required = false) String username, @RequestParam(required = false) String password) {
        
    	serviceBean.abortProcess(processId, username, password);
        Map<String, Object> processMap = serviceBean.getList("runningProcess", username, password);
        map.addAttribute("processMap", processMap);
        map.addAttribute("urlEncoder", serviceBean);
        return "joget/runningProcess";
    }
    
    //get history
    @RequestMapping("/joget/history")
    public String getHistory(ModelMap map, @RequestParam(required = false) String username, @RequestParam(required = false) String password) {
        Map<String, Object> processMap = serviceBean.getList("history", username, password);
        map.addAttribute("processMap", processMap);
        map.addAttribute("urlEncoder", serviceBean);
        return "joget/history";
    }
    
    //start process
    @RequestMapping("/joget/process/start/{processDefId}")
    public String startProcess(ModelMap map, @PathVariable String processDefId, @RequestParam(required = false) String username, @RequestParam(required = false) String password) {
        String processId = serviceBean.startProcess(processDefId, username, password);
        System.out.println(username+"--------"+password);
        map.addAttribute("processId", processId);
        return "joget/startProcess";
    }
    
    //configure base url
    @RequestMapping("/joget/configure")
    public String configure(HttpServletRequest request, @RequestParam(required = false) String baseUrl) {
        if (baseUrl != null && baseUrl.trim().length() > 0) {
            serviceBean.setBaseUrl(baseUrl);
            request.getSession(true).setAttribute("baseUrl", baseUrl);
        } else {
            request.getSession(true).removeAttribute("baseUrl");
        }
        String referer = request.getHeader("Referer");
        return "redirect:" + referer;
    }
    
}
